package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.UserActiveService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Slf4j
@Service
@GlobalTransactional
public class UserActiveServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements UserActiveService {
    @Autowired
    ApUserMapper apUserMapper;
    /**
     * 用户激活  解锁   修改status状态 0正常  1锁定
     * @param id  前端传过来ApUser的id
     * @return
     */
    @Override
    public ResponseResult active(Integer id) {
        //1、参数校验
        if(id==null){
            log.error("根据用户id查询用户不存在失败id为：{id}",id);
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2、查询出 ApUser 数据
        LambdaQueryWrapper<ApUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUser::getId,id);
        //ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getId, id));
        ApUser apUser = apUserMapper.selectOne(wrapper);
        log.error("前status：{}",apUser.getStatus());
        if(apUser==null){
            log.error("用户不存在");
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        apUser.setStatus(false);
        boolean update = this.update(apUser,wrapper);//更新到数据库
        log.error("后status：{}",apUser.getStatus());
        if(!update){
            log.error("更改用户状态失败");
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        return ResponseResult.okResult("激活成功");
    }

    /**
     * 注入发送邮件对象
     */
    @Autowired
    private JavaMailSender mailSender;
    /**
     * 注入发送发的邮件地址
     */
    @Value("${spring.mail.username}")
    private String from;

    @Override
    public ResponseResult sendMsg(Integer id) {
        //1、参数校验
        if(id==null){
            log.error("根据用户id查询用户不存在失败id为：{id}",id);
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2、查询出 ApUser 数据
        ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getId, id));
        if(apUser==null){
            log.error("用户不存在");
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //构建邮件消息对象
        MimeMessage message = mailSender.createMimeMessage();
        //构建邮件消息包装类，方便参数设置
        MimeMessageHelper messageHelper = null;
        try {
            messageHelper = new MimeMessageHelper(message, true);
        } catch (MessagingException e) {
            log.error("调用激活邮箱失败 原因：{}",e);
            e.printStackTrace();
        }
        //设置mail发送方地址
        try {
            messageHelper.setFrom(from);
        } catch (MessagingException e) {
            log.error("发送方发送邮件失败 原因：{}",e);
            e.printStackTrace();
        }
        //设置mail接收方地址
        try {
            messageHelper.setTo(apUser.getEmail());
        } catch (MessagingException e) {
            log.error("接受方接受邮件失败 原因：{}",e);
            e.printStackTrace();
        }
        String subject="黑马头条激活邮件";
        //设置mail主题
        try {
            messageHelper.setSubject(subject);
        } catch (MessagingException e) {
            log.error("");
            e.printStackTrace();
        }
        //设置正文
//        String text = "<a href='192.168.147.54:8082/user/api/v1/active?id="+apUser.getId()+"'>激活账户</a>";
        String text = "<a href='http://localhost:9002/api/v1/active?id="+apUser.getId()+"'>激活账户</a>";
        //active(id); //调用激活方法
        /**
         * 参数1：邮件正文
         * 参数2：true表示html格式
         */
        try {
            messageHelper.setText(text,true);
        } catch (MessagingException e) {
            log.error("获取邮件内容失败 原因：{}",e);
            e.printStackTrace();
        }
        mailSender.send(message);
        return ResponseResult.okResult("发送邮件成功");

    }
}
